﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCHSHue.DAL;
namespace TCHSHue.Modules
{
    public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var user = (User)Session["userDangNhap"];
            if (user != null)
            {
                #region Commented
                //if (user.vaitro != null && user.vaitro.Value == (int)VAITRO.Admin)
                //{
                //    lbUser.Text = user.ho + " " + user.ten;
                //    daDangNhapDiv.Visible = true;
                //}
                //else
                //{
                //    Response.Redirect("~/Modules/Index.aspx");
                //    return;
                //} 
                #endregion

                lbUser.Text = user.hoTen;
                daDangNhapDiv.Visible = true;
            }
            else
            {
                mainmenu.Visible = false;
                Control dangKy = null;
                if (Request.QueryString["mode"] == "quenmatkhau")
                    dangKy = LoadControl("~/Modules/AdminControl/DangNhap/ctrQuenMatKhau.ascx");
                else
                    dangKy = LoadControl("~/Modules/AdminControl/DangNhap/ctrDangNhap.ascx");
                content.Controls.Add(dangKy);
                return;
            }

            string mode = Request.QueryString["mode"];
            Control ctr = null;
            if (!string.IsNullOrEmpty(mode))
            {
                switch (mode)
                {
                    case "0":
                        Session["userDangNhap"] = null;
                        Response.Redirect("~/Modules/Admin.aspx");
                        break;
                    //Phường
                    case "phuong":
                        ctr = LoadControl("~/Modules/AdminControl/Phuong/ctrPhuongList.ascx");
                        break;
                    case "themphuong":
                    case "chinhphuong":
                        ctr = LoadControl("~/Modules/AdminControl/Phuong/ctrPhuongCreate.ascx");
                        break;
                    //Trạng thái hồ sơ
                    case "trangthaihoso":
                        ctr = LoadControl("~/Modules/AdminControl/TrangThai/ctrTrangThaiHoSoList.ascx");
                        break;
                    case "themtrangthaihoso":
                    case "chinhtrangthaihoso":
                        ctr = LoadControl("~/Modules/AdminControl/TrangThai/ctrTrangThaiHoSoCreate.ascx");
                        break;
                    //Liên kết website
                    case "lkwebsite":
                        ctr = LoadControl("~/Modules/AdminControl/LKWebSite/ctrLKWSList.ascx");
                        break;
                    case "themlk":
                    case "chinhlk":
                        ctr = LoadControl("~/Modules/AdminControl/LKWebSite/ctrLKWSCreate.ascx");
                        break;
                    //Slide ảnh
                    case "slideimage":
                        ctr = LoadControl("~/Modules/AdminControl/SlideAnh/ctrSlideImageList.ascx");
                        break;
                    case "themslideimage":
                    case "chinhslideimage":
                        ctr = LoadControl("~/Modules/AdminControl/SlideAnh/ctrSlideImageCreate.ascx");
                        break;
                    //Thay đổi mật khẩu
                    case "doimatkhau":
                        ctr = LoadControl("~/Modules/AdminControl/DangNhap/ctrDoiMatKhau.ascx");
                        break;
                    //Quên mật khẩu
                    case "quenmatkhau":
                        ctr = LoadControl("~/Modules/AdminControl/DangNhap/ctrQuenMatKhau.ascx");
                        break;
                    //DS Bài viết
                    case "baiviet":
                        ctr = LoadControl("~/Modules/AdminControl/BaiViet/ctrBaiVietList.ascx");
                        break;
                    //Thêm mới/Chỉnh sửa bài viết
                    case "thembaiviet":
                    case "chinhbaiviet":
                        ctr = LoadControl("~/Modules/AdminControl/BaiViet/ctrBaiVietCreate.ascx");
                        break;
                    case "taianh":
                        ctr = LoadControl("~/Modules/AdminControl/AnhBaiViet/ctrAnh.ascx");
                        break;
                    case "taitailieu":
                        ctr = LoadControl("~/Modules/AdminControl/BieuMau/ctrBieuMau.ascx");
                        break;
                    case "user":
                        ctr = LoadControl("~/Modules/AdminControl/User/ctrUserList.ascx");
                        break;
                    case "themuser":
                        ctr = LoadControl("~/Modules/AdminControl/User/ctrUserCreate.ascx");
                        break;
                    case "editprofile":
                        ctr = LoadControl("~/Modules/AdminControl/User/ctrUserEdit.ascx");
                        break;
                    case "hoidap":
                        ctr = LoadControl("~/Modules/AdminControl/HoiDap/ctrHoiDapList.ascx");
                        break;
                    case "chinhhoidap":
                        ctr = LoadControl("~/Modules/AdminControl/HoiDap/ctrHoiDapEdit.ascx");
                        break;
                    case "hoso":
                        ctr = LoadControl("~/Modules/AdminControl/HoSo/ctrHoSo.ascx");
                        break;

                    default:
                        Response.Redirect("~/Modules/Admin.aspx");
                        break;
                }
            }
            if (ctr != null)
                content.Controls.Add(ctr);
            createMenu(mode);
        }
        void createMenu(string mode)
        {
            bool[] menu = new bool[10];
            switch (mode)
            {
                case "phuong":
                case "themphuong":
                case "chinhphuong":
                    menu[0] = true;
                    break;
                case "trangthaihoso":
                case "themtrangthaihoso":
                case "chinhtrangthaihoso":
                    menu[1] = true;
                    break;
                case "lkwebsite":
                case "themlk":
                case "chinhlk":
                    menu[2] = true;
                    break;
                case "slideimage":
                case "themslideimage":
                case "chinhslideimage":
                    menu[3] = true;
                    break;
                case "baiviet":
                case "thembaiviet":
                case "chinhbaiviet":
                    menu[4] = true;
                    break;
                case "taianh":
                    menu[5] = true;
                    break;
                case "taitailieu":
                    menu[6] = true;
                    break;
                case "user":
                    menu[7] = true;
                    break;
                case "themuser":
                    menu[7] = true;
                    break;
                case "hoidap":
                    menu[8] = true;
                    break;
                case "hoso":
                    menu[9] = true;
                    break;
            }
            string style = "class='selected' style='color:#326F9F'";
            string nonstyle = "class='unselected'";
            string strMenu = string.Format(@" <ul>
                                                <li><a {8} href='Admin.aspx?mode=user' title=''>User</a></li>
                                                <li><a {0} href='Admin.aspx?mode=phuong' title=''>Phường</a></li>
                                                <li><a {1} href='Admin.aspx?mode=trangthaihoso'>Trạng thái hồ sơ</a></li>
                                                <li><a {2} href='Admin.aspx?mode=lkwebsite'>Liên kết Website</a></li>                                                
                                                <li><a {4} href='Admin.aspx?mode=baiviet'>Bài viết</a></li>
                                                <li><a {3} href='Admin.aspx?mode=slideimage'>Slide ảnh</a></li>
                                                <li><a {5} href='Admin.aspx?mode=taianh'>Tải tài nguyên bài viết</a></li>
                                                <li><a {6} href='Admin.aspx?mode=taitailieu'>Tải tài liệu</a></li>
                                                <li><a {7} href='Admin.aspx?mode=hoidap' title=''>Hỏi đáp</a></li>
                                                
                </ul>", menu[0] ? style : nonstyle, menu[1] ? style : nonstyle, menu[2] ? style : nonstyle,
                      menu[3] ? style : nonstyle, menu[4] ? style : nonstyle, menu[5] ? style : nonstyle,
                      menu[6] ? style : nonstyle, menu[8] ? style : nonstyle, menu[7] ? style : nonstyle);
            submainmenu.InnerHtml = strMenu;
        }
        //<li><a {7} href='Admin.aspx?mode=user' title=''>Người dùng</a></li>
        //<li><a {9} href='Admin.aspx?mode=hoso' title=''>Hồ sơ</a></li>
    }
}